package com.weme.chat.view

import android.content.Context
import android.graphics.Rect
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.ViewConfiguration
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.customview.widget.ViewDragHelper
import com.weme.chat.R
import java.lang.Math.abs

interface OnIconClickListener {
    fun onIconClick()
}

class MyViewDragConstraintLayout(context: Context, attrs: AttributeSet?) : ConstraintLayout(context, attrs) {
    private lateinit var mViewDragHelper: ViewDragHelper
    private lateinit var mIconView: ImageView
    private var mDownX: Float = 0f
    private var mDownY: Float = 0f
    private val mTouchSlop: Int = ViewConfiguration.get(context).scaledTouchSlop

    private var iconClickListener: OnIconClickListener? = null

    fun setOnIconClickListener(listener: OnIconClickListener) {
        iconClickListener = listener
    }


    init {

        mViewDragHelper = ViewDragHelper.create(this, 1.0f, object : ViewDragHelper.Callback() {
            override fun tryCaptureView(child: View, pointerId: Int): Boolean {
                Log.e("touch", "tryCaptureView")
             //   return child == mIconView
                return false
            }

            override fun clampViewPositionVertical(child: View, top: Int, dy: Int): Int {
                return if (top < 100) 100 else if (top > height - child.height) height - child.height else top
            }

            override fun clampViewPositionHorizontal(child: View, left: Int, dx: Int): Int {

                return if (left < 0) 0 else if (left > width - child.width) width - child.width else left
            }

            override fun onViewCaptured(capturedChild: View, activePointerId: Int) {
                super.onViewCaptured(capturedChild, activePointerId)

            }

            override fun onViewReleased(releasedChild: View, xvel: Float, yvel: Float) {
                super.onViewReleased(releasedChild, xvel, yvel)

            }

            override fun onEdgeDragStarted(edgeFlags: Int, pointerId: Int) {
                super.onEdgeDragStarted(edgeFlags, pointerId)
                mViewDragHelper.captureChildView(mIconView, pointerId)
            }
        })

        mViewDragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT)
    }

    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
        val x = ev.x
        val y = ev.y
        val iconViewRect = Rect()
        mIconView.getGlobalVisibleRect(iconViewRect)

        // 检查触摸事件是否在imCall视图范围内
        if (iconViewRect.contains(x.toInt(), y.toInt())) {
            // 如果在范围内，返回true表示拦截该事件，交由onTouchEvent处理
            return true
        }

        // 否则交由ViewDragHelper处理
        return mViewDragHelper.shouldInterceptTouchEvent(ev)
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        mViewDragHelper.processTouchEvent(event)
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                mDownX = event.x
                mDownY = event.y
            }
            MotionEvent.ACTION_MOVE -> {
                val deltaX = abs(event.x - mDownX)
                val deltaY = abs(event.y - mDownY)
                if (deltaX > mTouchSlop || deltaY > mTouchSlop) { // 移动距离超过阈值，判断为拖动事件，不处理点击事件
                    return true
                }
            }
            MotionEvent.ACTION_UP -> {
                val deltaX = abs(event.x - mDownX)
                val deltaY = abs(event.y - mDownY)
                if (deltaX <= mTouchSlop && deltaY <= mTouchSlop) { // 移动距离未超过阈值，判断为点击事件
                    iconClickListener?.onIconClick()
                }
            }
        }
        return true
    }


    override fun onFinishInflate() {
        super.onFinishInflate() // Assuming 'imCall' is the id of your draggable view
        mIconView = findViewById(R.id.imCall)
    }
}
